package com.roy.minimq.remoting.domain.netty;

import com.roy.minimq.remoting.domain.entity.RemotingCommand;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;

/**
 * @author ：楼兰
 * @date ：Created in 2021/8/9
 * @description:
 **/
@ChannelHandler.Sharable
public class NettyEncoder extends MessageToByteEncoder<RemotingCommand> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, RemotingCommand remotingCommand, ByteBuf byteBuf) throws Exception {
        remotingCommand.encodeInto(byteBuf);
        logger.info("NettyDecoder encoder: byteBuf "+byteBuf.toString(Charset.forName("utf-8")));
    }
}
